import 'package:flutter/material.dart';
import 'package:flutter_bilibili/core/extension/int_extension.dart';
import 'package:flutter_bilibili/core/event_bus/event_bus.dart';
import 'package:flutter_bilibili/ui/pages/charts/charts.dart';
import 'package:flutter_bilibili/ui/pages/publish/publish.dart';
import 'package:flutter_speed_dial/flutter_speed_dial.dart';
import 'package:jpush_flutter/jpush_flutter.dart';
import 'package:permission_handler/permission_handler.dart';
import '../push/push.dart';
import 'initialize_main.dart';

class HYMainScreen extends StatefulWidget {
  ///起始路由
  static const String routeName = "/";

  @override
  State<HYMainScreen> createState() => _HYMainScreenState();
}

class _HYMainScreenState extends State<HYMainScreen> {
  int _currentIndex = 0;

  ///极光推送
  JPush jpush = JPush();

  ///当前显示的page编号
  @override
  void initState() {
    mineEventBus.on<int>().listen((event) {
      ///接收事件总线传来的数据，主要用于跳转至“我的”页面
      setState(() {
        _currentIndex = event;
      });
    });

    ///获取多个权限
    requestPermissions();

    super.initState();
  }

  ///权限获取
  void requestPermissions() async {
    Map<Permission, PermissionStatus> statuses = await [
      Permission.phone,
      Permission.storage,
      Permission.location,
      Permission.notification,
    ].request();
    // print('电话权限：${statuses[Permission.notification]}');
    // print('存储权限：${statuses[Permission.storage]}');
    // print('定位权限：${statuses[Permission.location]}');
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: IndexedStack(
        ///显示哪一个页面
        index: _currentIndex,

        ///显示那些页面
        children: mainIndexStackPages,
      ),
      bottomNavigationBar: BottomNavigationBar(
        ///底部导航栏
        selectedFontSize: 10.px,

        ///选中时字体大小
        unselectedFontSize: 10.px,

        ///未选中时字体大小
        selectedItemColor: Color.fromRGBO(210, 83, 125, 1),

        ///选中时字体颜色
        type: BottomNavigationBarType.fixed,

        ///显示label标签，而不是隐藏label
        currentIndex: _currentIndex,

        ///当前显示的页面
        items: mainIndexStackItems,
        onTap: (index) {
          ///发布界面
          if (index == 2) {
            Navigator.of(context).pushNamed(HYPublishScreen.routeName);
          } else {
            setState(() {
              _currentIndex = index;
            });
          }
        },
      ),
      floatingActionButton: SpeedDial(
        icon: Icons.star_rate_sharp,
        backgroundColor: Theme.of(context).canvasColor,
        children: [
          SpeedDialChild(
            onTap: () {
              Navigator.of(context).pushNamed(HYChartScreen.routeName);
            },
            backgroundColor: Colors.white,
            label: '统计',
            child: ImageIcon(
              AssetImage("assets/image/icon/charts_custom.png"),
              size: 20.px,
            ),
          ),
          SpeedDialChild(
            backgroundColor: Colors.white,
            onTap: () {
              Navigator.of(context).pushNamed(HYPushScreen.routeName);
            },
            label: '推送',
            child: Icon(
              Icons.announcement_sharp,
              size: 20.px,
            ),
          ),
        ],
      ),
      // floatingActionButton: FloatingActionButton(
      //   ///当有多个按钮时，设置一个Tag区别
      //   heroTag: 'charts',
      //   onPressed: () {
      //     Navigator.of(context).pushNamed(HYChartScreen.routeName);
      //   },
      //   child: ImageIcon(
      //     AssetImage("assets/image/icon/charts_custom.png"),
      //     size: 20.px,
      //   ),
      // ),
    );
  }
}
